package com.gsafety.wellsfort.service.common.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gsafety.wellsfort.common.util.EnvironmentUtil;
import com.gsafety.wellsfort.service.common.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class CityServiceImpl implements CityService {


    private Map<Integer, String> cache = new HashMap<>();

    @Override
    public String getCityNameByCode(Integer code) {
        if (!cache.containsKey(code)) {
            initCache();
        }
        return cache.get(code);
    }

    @Override
    public Integer getCityCodeByName(String name) {
        if (!cache.containsValue(name)) {
            initCache();
        }
        for (Map.Entry<Integer, String> entry : cache.entrySet()) {
            if (entry.getValue().equals(name)) {
                return entry.getKey();
            }
        }
        return null;
    }

    /**
     * 初始化数据
     */
    private void initCache() {
        try {
            File file = null;
            if(EnvironmentUtil.isProduct()) {
                 file = new File("/www/wwwroot/javajiaoyu/city.json");
            }else{
                file  = new File("/alidata/www/jiaoyujava/city.json");
            }
            InputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            String jsonString = new String(bytes);
            JSONArray jsonArray = JSONArray.parseArray(jsonString);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject != null) {
                    cache.put(Integer.valueOf(jsonObject.get("code").toString()), jsonObject.get("name").toString());
                    JSONArray cityArray = jsonObject.getJSONArray("cityList");
                    if (cityArray != null && cityArray.size() > 0) {
                        for (int j = 0; j < cityArray.size(); j++) {
                            JSONObject cityObject = cityArray.getJSONObject(j);
                            if (cityObject != null) {
                                cache.put(Integer.valueOf(cityObject.get("code").toString()), cityObject.get("name").toString());
                                JSONArray areaArray = cityObject.getJSONArray("areaList");
                                if (areaArray != null && areaArray.size() > 0) {
                                    for (int t = 0; t < areaArray.size(); t++) {
                                        JSONObject areaObject = areaArray.getJSONObject(t);
                                        cache.put(Integer.valueOf(areaObject.get("code").toString()), areaObject.get("name").toString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("exception={}", e);
        } finally {
        }
    }
}
